/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_Colour_h__
#define __dylab_Colour_h__

//===========================================================================================
#include <dylab/utils/Array.h>
#include <dylab/utils/types/TypeParser.h>

//===========================================================================================
namespace dylab {

    class DYLAB_EXPORT Colour
    {
    public:
        static const Colour WHITE;
        static const Colour BLACK;

    private:
        real_t mRed, mGreen, mBlue, mAlpha;

    public:
        Colour()
                { }
        Colour(real_t _r, real_t _g, real_t _b, real_t _a = DYLAB_R(1))
            : mRed(_r), mGreen(_g), mBlue(_b), mAlpha(_a)
                { }
        Colour(const ArrayReal<3> & _c)
            : mRed(_c[0]), mGreen(_c[1]), mBlue(_c[2]), mAlpha(DYLAB_R(1))
                { }

        Colour(const ArrayReal<4> & _c)
            : mRed(_c[0]), mGreen(_c[1]), mBlue(_c[2]), mAlpha(_c[3])
                { }

        real_t & r() 
                { return mRed; }
        const real_t & r() const 
                { return mRed; }

        real_t & g()
                { return mGreen; }
        const real_t & g() const 
                { return mGreen; }

        real_t & b()
                { return mBlue; }
        const real_t & b() const 
                { return mBlue; }

        real_t & a()
                { return mAlpha; }
        const real_t & a() const 
                { return mAlpha; }

        operator String() const
                { return String::fmt("%1% %2% %3% %4%", r(), g(), b(), a()); }
    };

    //===========================================================================================    
    inline std::ostream & operator << (std::ostream & _os, const Colour & _o)
            { return _os << String(_o); }

    //===========================================================================================
    template <>
        inline Colour TypeParser::parse(const String & _string)
            { 
                try { return Colour(ArrayReal<4>::parse(_string)); } 
                catch (EInvalidFormat &) { return Colour(ArrayReal<3>::parse(_string)); }
            }
    template <>
        inline Colour TypeParser::parse(const String & _string, const Colour & _defaultValue)
            {
                try { return TypeParser::parse<Colour>(_string); }
                catch (EInvalidFormat &) { return _defaultValue; }
            }
}
//===========================================================================================
#endif // __dylab_Colour_h__
